package com.vehiclesecure.config;

import com.vehiclesecure.model.Vehicle;
import com.vehiclesecure.repository.VehicleRepository;
import com.vehiclesecure.service.VehicleUserSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 测试车辆用户同步
 * 创建测试数据来验证同步功能
 */
@Component
@Order(1) // 在同步之前创建测试数据
public class TestVehicleUserSync implements CommandLineRunner {

    private final VehicleRepository vehicleRepository;
    @Autowired
    public TestVehicleUserSync(VehicleRepository vehicleRepository, 
                              VehicleUserSyncService vehicleUserSyncService) {
        this.vehicleRepository = vehicleRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("创建测试车辆数据...");
        
        // 创建测试车辆1 - 有邮箱但无用户
        if (!vehicleRepository.existsByLicensePlate("TEST001")) {
            Vehicle vehicle1 = new Vehicle();
            vehicle1.setVin("TESTVIN00112345678");
            vehicle1.setLicensePlate("TEST001");
            vehicle1.setBrand("大众");
            vehicle1.setModel("帕萨特");
            vehicle1.setColor("黑色");
            vehicle1.setYear(2023);
            vehicle1.setFuelType("汽油");
            vehicle1.setTransmissionType("自动");
            vehicle1.setEngineCapacity(2.0);
            vehicle1.setVehicleType("轿车");
            vehicle1.setOwnerName("张三");
            vehicle1.setOwnerEmail("zhangsan@test.com");
            vehicle1.setOwnerPhone("13800138001");
            vehicle1.setRegistrationDate(LocalDateTime.now().minusMonths(6));
            vehicle1.setInsuranceExpiry(LocalDateTime.now().plusMonths(6));
            vehicle1.setLastMaintenance(LocalDateTime.now().minusMonths(1));
            vehicle1.setMileage(5000);
            vehicle1.setStatus("ACTIVE");
            vehicle1.setIsActive(true);
            vehicle1.setIsStolen(false);
            vehicle1.setCreatedAt(LocalDateTime.now());
            vehicle1.setUpdatedAt(LocalDateTime.now());
            
            vehicleRepository.save(vehicle1);
            System.out.println("创建测试车辆1: " + vehicle1.getLicensePlate());
        }

        // 创建测试车辆2 - 无邮箱无用户
        if (!vehicleRepository.existsByLicensePlate("TEST002")) {
            Vehicle vehicle2 = new Vehicle();
            vehicle2.setVin("TESTVIN00212345678");
            vehicle2.setLicensePlate("TEST002");
            vehicle2.setBrand("宝马");
            vehicle2.setModel("X5");
            vehicle2.setColor("白色");
            vehicle2.setYear(2023);
            vehicle2.setFuelType("汽油");
            vehicle2.setTransmissionType("自动");
            vehicle2.setEngineCapacity(3.0);
            vehicle2.setVehicleType("SUV");
            vehicle2.setOwnerName("李四");
            vehicle2.setOwnerPhone("13800138002");
            vehicle2.setRegistrationDate(LocalDateTime.now().minusMonths(3));
            vehicle2.setInsuranceExpiry(LocalDateTime.now().plusMonths(9));
            vehicle2.setLastMaintenance(LocalDateTime.now().minusMonths(1));
            vehicle2.setMileage(8000);
            vehicle2.setStatus("ACTIVE");
            vehicle2.setIsActive(true);
            vehicle2.setIsStolen(false);
            vehicle2.setCreatedAt(LocalDateTime.now());
            vehicle2.setUpdatedAt(LocalDateTime.now());
            
            vehicleRepository.save(vehicle2);
            System.out.println("创建测试车辆2: " + vehicle2.getLicensePlate());
        }

        // 创建测试车辆3 - 有邮箱但邮箱格式不完整
        if (!vehicleRepository.existsByLicensePlate("TEST003")) {
            Vehicle vehicle3 = new Vehicle();
            vehicle3.setVin("TESTVIN00312345678");
            vehicle3.setLicensePlate("TEST003");
            vehicle3.setBrand("奥迪");
            vehicle3.setModel("A6");
            vehicle3.setColor("银色");
            vehicle3.setYear(2023);
            vehicle3.setFuelType("汽油");
            vehicle3.setTransmissionType("自动");
            vehicle3.setEngineCapacity(2.0);
            vehicle3.setVehicleType("轿车");
            vehicle3.setOwnerName("王五");
            vehicle3.setOwnerPhone("13800138003");
            vehicle3.setRegistrationDate(LocalDateTime.now().minusMonths(2));
            vehicle3.setInsuranceExpiry(LocalDateTime.now().plusMonths(10));
            vehicle3.setLastMaintenance(LocalDateTime.now().minusMonths(1));
            vehicle3.setMileage(3000);
            vehicle3.setStatus("ACTIVE");
            vehicle3.setIsActive(true);
            vehicle3.setIsStolen(false);
            vehicle3.setCreatedAt(LocalDateTime.now());
            vehicle3.setUpdatedAt(LocalDateTime.now());
            
            vehicleRepository.save(vehicle3);
            System.out.println("创建测试车辆3: " + vehicle3.getLicensePlate());
        }

        System.out.println("测试车辆数据创建完成！");
    }
}